﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommunicationLib.Helper
{
    public class Log
    {
        public static string path = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";// 日志文件路径
        public static object loglock = new object();//锁对象
        public static void Debug(string content)
        {
            WriteLog("Debug", content);
        }
        public static void Info(string content)
        {
            WriteLog("Info", content);
        }


        public static void Error(string content)
        {
            WriteLog("Error", content);
        }


        protected static void WriteLog(string type, string content)
        {
            lock (loglock)
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                StreamWriter streamWriter = File.AppendText(filename);// 打开文件流
                string text = time + " " + type + ": " + content + Environment.NewLine;
                streamWriter.Write(text);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }

    }
}
